<?php

class fluxSelect {

		  private $fromTables     = array();
		  private $givenFields    = array();
		  private $whereCondition = array();
		  public  $having         = NULL;
		  private $mainObj        = NULL;

		  public function __construct ($link,$method,$fields,$mainObj) {

					 $this->mainObj=$mainObj;
					 $this->givenFields=$this->mainObj->parseFields($fields);

		  }

		  public function from ($tables) {
				$tables=func_get_args();
				$this->fromTables[]=$this->mainObj->prepareTables($tables);
		  }


		  public function getResource(){
					 return $this->mainObj->doQuery($this->__toString());
		  }

		  public function getObject(){
					 return $this->mainObj->parseQueryAsObject($this->mainObj->doQuery($this->__toString()));
		  }

		  public function getArray ($query=NULL) {

			if (!is_null($query))
				return $this->mainObj->parseQueryAsArray($this->mainObj->doQuery($query));

			return $this->mainObj->parseQueryAsArray($this->mainObj->doQuery($this->__toString()));

		  }

		  public function getBoolean () {
					 return mysql_num_rows ($this->mainObj->doQuery($this->__toString()));
		  }

		  public function q2f () { // Dumps the whole result to a file
					 //INTO OUTFILE '/home/lalalal/uoo.txt'
		  }



		  //**********************************************
		  //DUMPS
		  //**********************************************

		  public function dumpFields ($die=NULL){
					 echo 'Field List in Query:' . "\n";
					 var_dump ($this->givenFields);
					 echo "\n";
					 if ($die) die();
		  }

		  public function dumpTables($die=NULL){
					 echo 'Table List in Query:' . "\n";
					 var_dump ($this->fromTables);
					 if ($die) die();
						
		  }

		  public function dumpWhere($die=NULL){
					 echo 'Where Specification Dump:' . "\n";
					 var_dump ($this->whereCondition);
					 echo "\n";
					 if ($die) die();
		  }

		  //**********************************************


		  private function myunion ($obj){

					 return ' UNION ' . $obj->__toString();

		  }

		  public function having ($arguments=NULL) {

				if (!is_object($this->having))
					$this->having = new fluxHaving ($arguments,$this->mainObj);

			}

		  public function __call($method,$arguments) {

					 $this->whereCondition[]=$this->mainObj->setWhere($method,$arguments);

		  }

		  public function __toString(){

				return($this->parseQueryAsString());

		  }

		  public function getHaving () {

					 if(!is_object($this->having))
								return;

					 return $this->having->__toString();

		  }

		  private function parseQueryAsString(){

					$selectFrom = $this->mainObj->parsePart('SELECT',$this->givenFields).
								     $this->mainObj->parsePart('FROM',$this->fromTables);

					if (is_object($this->having) && !sizeof($this->whereCondition))
						return $selectFrom . ' ' . $this->getHaving();

					$selectFrom.=$this->mainObj->parseWherePart($this->whereCondition);
					$selectFrom.=$this->getHaving();

					return ($selectFrom);

		  }


		}//EOC

?>
